﻿@using Citi.HeadCount.Domain
@model System.Collections.Generic.IEnumerable<Citi.HeadCount.WebUI.Models.RuleResultViewModel>
@{
    ViewBag.Title = "Rules";
}
<script type="text/javascript">
    $(function () {
        $("#OpenDialogApplyRules").click(function () {
            $("#divApplyRules").dialog('open');
            //$(".ui-datepicker-calendar").hide();
        });

        $("#divApplyRules").dialog({
            modal: true,
            autoOpen: false
        });

        $("#referenceDate").datepicker({
            changeMonth: true,
            changeYear: true,
            showButtonPanel: true,
            dateFormat: 'MM yy'
        });
    });
</script>
<h2>
    Rules</h2>
<p class="floatLeft">
    @Html.ActionLink("Create New", "Create")
</p>
<div id="divApplyRules" title="Apply Rules">
    Fill a date to apply rules
    @using (Html.BeginForm("ApplyRules", "RuleResult"))
    {
        @Html.TextBox("referenceDate")
        <br /><br />
        <input type="submit" value="Apply rules" />
    }
</div>
<p class="floatRight">
    @*@Html.ActionLink("Apply Rules", "ApplyRules")*@
    <a href="#" id="OpenDialogApplyRules">Apply Rules</a>
</p>
<div class="clear">
</div>
<table>
    <tr>
        <th>
            Action
        </th>
        <th>@RuleType.OU.GetDescription()
        </th>
        <th>@RuleType.LVID.GetDescription()
        </th>
        <th>@RuleType.ManagedSegment.GetDescription()
        </th>
        <th>@RuleType.Function.GetDescription()
        </th>
        <th>@TypeResultType.OTFlag.GetDescription()
        </th>
        <th>@TypeResultType.Business.GetDescription()
        </th>
        <th>@TypeResultType.Business1.GetDescription()
        </th>
        <th>@TypeResultType.Area.GetDescription()
        </th>
        <th>@TypeResultType.DeckRegional.GetDescription()
        </th>
        <th>@TypeResultType.DeckRegional1.GetDescription()
        </th>
        <th>@TypeResultType.Local.GetDescription()
        </th>
        <th>@TypeResultType.FPAManager.GetDescription()
        </th>
        <th>@TypeResultType.FPAAnalyst.GetDescription()
        </th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.ActionLink("Delete", "Delete", "RuleResult", new { id = item.Id }, null)
            </td>
            <td>@(item.Rules.Any(x => x.Type == (int)RuleType.OU) ? @item.Rules.First(x => x.Type == (int)RuleType.OU).Value : "-")
            </td>
            <td>@(item.Rules.Any(x => x.Type == (int)RuleType.LVID) ? @item.Rules.First(x => x.Type == (int)RuleType.LVID).Value : "-")
            </td>
            <td>
                @foreach (var managedSegment in item.Rules.Where(x => x.Type == (int)RuleType.ManagedSegment).OrderBy(x => x.Level))
                {
                    @managedSegment.Value
                    <br />
                }
            </td>
            <td>
                @foreach (var function in item.Rules.Where(x => x.Type == (int)RuleType.Function).OrderBy(x => x.Level))
                {
                    @function.Value
                    <br />
                }
            </td>
            <td>
                @{ var typeRuleResults = item.TypeRuleResults;
                   var type = typeRuleResults.FirstOrDefault(x => x.TypeResult == TypeResultType.OTFlag.GetDescription());}
                @(type != null ? type.TypeResultValue : "-")
            </td>
            <td>
                @{ type = typeRuleResults.FirstOrDefault(x => x.TypeResult == TypeResultType.Business.GetDescription());}
                @(type != null ? type.TypeResultValue : "-")
            </td>
            <td>
                @{ type = typeRuleResults.FirstOrDefault(x => x.TypeResult == TypeResultType.Business1.GetDescription());}
                @(type != null ? type.TypeResultValue : "-")
            </td>
            <td>
                @{ type = typeRuleResults.FirstOrDefault(x => x.TypeResult == TypeResultType.Area.GetDescription());}
                @(type != null ? type.TypeResultValue : "-")
            </td>
            <td>
                @{ type = typeRuleResults.FirstOrDefault(x => x.TypeResult == TypeResultType.DeckRegional.GetDescription());}
                @(type != null ? type.TypeResultValue : "-")
            </td>
            <td>
                @{ type = typeRuleResults.FirstOrDefault(x => x.TypeResult == TypeResultType.DeckRegional1.GetDescription());}
                @(type != null ? type.TypeResultValue : "-")
            </td>
            <td>
                @{ type = typeRuleResults.FirstOrDefault(x => x.TypeResult == TypeResultType.Local.GetDescription());}
                @(type != null ? type.TypeResultValue : "-")
            </td>
            <td>
                @{ type = typeRuleResults.FirstOrDefault(x => x.TypeResult == TypeResultType.FPAManager.GetDescription());}
                @(type != null ? type.TypeResultValue : "-")
            </td>
            <td>
                @{ type = typeRuleResults.FirstOrDefault(x => x.TypeResult == TypeResultType.FPAAnalyst.GetDescription());}
                @(type != null ? type.TypeResultValue : "-")
            </td>
        </tr>
    }
</table>
